home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
60750
/
60750.xpi
/
surfcanyon_sc.xpi
/
content
/
xmlhttprequester.js
< prev
Wrap
Text File
|
2009-11-12
|
2KB
|
81 lines
function surfcanyon_xmlhttpRequester(unsafeContentWin, chromeWindow) {
this.unsafeContentWin = unsafeContentWin;
this.chromeWindow = chromeWindow;
}
// this function gets called by user scripts in content security scope to
// start a cross-domain xmlhttp request.
//
// details should look like:
// {method,url,onload,onerror,onreadystatechange,headers,data}
// headers should be in the form {name:value,name:value,etc}
surfcanyon_xmlhttpRequester.prototype.contentStartRequest = function(details) {
var url = details.url;
this.chromeWindow.setTimeout(
surfcanyon_gmCompiler.hitch(this, "chromeStartRequest", url, details), 0);
}
// this function is intended to be called in chrome's security context, so
// that it can access other domains without security warning
surfcanyon_xmlhttpRequester.prototype.chromeStartRequest=function(url, details) {
var req = new this.chromeWindow.XMLHttpRequest();
this.setupRequestEvent(this.unsafeContentWin, req, "onload", url, details);
this.setupRequestEvent(this.unsafeContentWin, req, "onerror", url, details);
this.setupRequestEvent(this.unsafeContentWin, req, "onreadystatechange", url, details);
req.open(details.method, url);
if (details.mimeType) {
req.overrideMimeType(details.mimeType);
}
if (details.headers) {
for (var prop in details.headers) {
req.setRequestHeader(prop, details.headers[prop]);
}
}
req.send(details.data);
}
// arranges for the specified 'event' on xmlhttprequest 'req' to call the
// method by the same name which is a property of 'details' in the content
// window's security context.
surfcanyon_xmlhttpRequester.prototype.setupRequestEvent =
function(unsafeContentWin, req, event, url, details) {
if (details[event]) {
req[event] = function() {
var responseHeaders = '';
var status = 0;
var statusText = '';
if (req.readyState == 4) {
try {
responseHeaders = req.getAllResponseHeaders();
status = req.status;
statusText = req.statusText;
} catch (e) {
}
}
var responseState = {
url: url,
responseText: req.responseText,
readyState: req.readyState,
responseHeaders: responseHeaders,
status: status,
statusText: statusText
}
// Pop back onto browser thread and call event handler.
new XPCNativeWrapper(unsafeContentWin, "setTimeout()").setTimeout(
function() {
details[event](responseState);
},
0);
}
}
}